<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb class="navigation">
      <el-breadcrumb-item :to="{ path: '/mapu' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 主体区域 -->
    <el-card class="notice">
      <h4>&nbsp;&nbsp;角色列表</h4>
      <!-- 角色列表区域 -->
      <el-table :data="roleList" border stripe>
        <!-- 展开列 -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row :class="['bdbottom', i1 === 0 ? 'bdtop' : '', 'vcenter']" v-for="(item1, i1) in scope.row.children"
              :key="item1.id">
              <!-- 渲染一级权限 -->
              <el-col :span="5">
                <el-tag closable @close="removeRightById(scope.row, item1.id)">{{ item1.permissionName }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 渲染二级和三级权限 -->
              <el-col :span="19">
                <!-- 通过for循环嵌套渲染二级权限 -->
                <el-row :class="[i2 === 0 ? '' : 'bdtop', 'vcenter']" v-for="(item2, i2) in item1.children"
                  :key="item2.id">
                  <el-col :span="6">
                    <el-tag type="success" closable @close="removeRightById(scope.row, item2.id)">{{ item2.permissionName }}
                    </el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="13">
                    <el-tag v-for="(item3) in item2.children" :key="item3.id" closable
                      @close="removeRightById(scope.row, item3.id)">
                      <el-tag type="waring">{{ item3.permissionName }}</el-tag>
                      <i class="el-icon-caret-right"></i>
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <!-- 索引列 -->
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="角色id" prop="roleId"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="删除角色" placement="top" :enterable="false">
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeUserById(scope.row.id)">
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'RolesList',
  data () {
    return {
      // 所有角色列表数据
      roleList: [],
      // 即将分配权限的ID值
      roleId: ''
    }
  },
  created () {
    this.getRolesList()
  },
  methods: {
    // 获取所有角色的列表
    async getRolesList () {
      const { data: res } = await this.$axios.get('admin/rpermission')
      if (res.code !== 200) {
        return this.$message.error('获取角色列表失败！')
      }
      this.roleList = res.data
      console.log(res)
    },
    // 删除角色
    async removeRole (row) {
      console.log(row.roleId)
      // 弹窗询问是否删除
      const confirmResult = await this.$confirm(
        '此操作将永久删除该角色, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
      // 如果取消删除，则返回值为字符串 cancel
      // 如果确认删除，则返回值为字符串 confirm
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      console.log('确认删除')
      const { data: res } = await this.$axios.delete('manager/system/role/deleteRole', { params: { roleId: row.roleId } })
      console.log(res)
      if (res.code === 200) {
        this.$message.success('删除角色成功！')
      } else if (res.code === 1007) {
        this.$message.error('请先删除该角色下的管理员！')
      } else if (res.code === 402) {
        this.$message.error('无操作权限！')
      } else { this.$message.error('删除角色失败！') }
      // this.roleList = res.data
      this.getRoleList()
    },
    // 根据id删除对应的权限
    async removeRightById (role, powerId) {
      const confirmResult = await this.$confirm(
        '此操作将永久删除该权限, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)// 如果取消删除，则返回值为字符串 cancel
      // 如果确认删除，则返回值为字符串 confirm
      // console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      console.log('确认删除')
      console.log(role)
      console.log(powerId)
      const { data: res } = await this.$axios.delete('manager/system/role/deleteMenuByRole', {
        params:
        {
          roleId: role.roleId,
          menuId: powerId
        }
      })
      console.log(res)
      if (res.code === 200) {
        this.$message.success('删除权限成功！')
      } else if (res.code === 402) {
        this.$message.error('无操作权限！')
      } else { this.$message.error('删除权限失败！') }
      role.children = res.data
    },
    // 获取三级权限并且渲染到树上
    getLeafKeys (node, arr) {
      // 没有子菜单则为三级权限
      if (!node.children) {
        return arr.push(node.id)
      }
      // children
      node.children.forEach(item =>
        this.getLeafKeys(item, arr))
    },
    // 监听分配权限对话框的关闭事件,更新节点数组为空
    setPowerDialogClosed () {
      this.defKeys = []
    }
  }
}
</script>

<style scoped>
.navigation {
  padding: 10px;
}

.notice {
  margin-bottom: 40px;
  border-radius: 15px;
  box-shadow: 15px 15px 10px -12px #4c61a0;
}

h4 {
  padding-bottom: 8px;
  margin-bottom: 20px;
  border-bottom: 2px solid #4c61a0;
}

/* 展开列的权限控制 */
.el-tag {
  margin: 7px;
}

.bdtop {
  border-top: 1px solid #eee;
}

.bdbottom {
  border-bottom: 1px solid #eee;
}

.vcenter {
  display: flex;
  align-items: center;
}
</style>
